package com.shen.accountbook.module.home.sortmanagefragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shen.accountbook.R;
import com.shen.accountbook.bean.temp.FirstSortBean;
import com.shen.accountbook.bean.temp.SecondSortBean;
import com.shen.accountbook.bean.temp.ThirdSortBean;
import com.shen.accountbook.module.base.BaseHolder;

import butterknife.BindView;

/**
 * author:  shen
 * date:    2018/9/7
 */
public class SortHolder<T> extends BaseHolder<T> {

    @BindView(R.id.tv_sort_itemsortmanage)
    TextView mTvSort;

    @BindView(R.id.layout_itemsortmanage)
    LinearLayout mLLayout;

    SortManageAdapter mAdapter;
    SortManageAdapter.ItemOnClickListen mListen;
    private ThirdSortBean mSort;

    public SortHolder(@NonNull View itemView, Context context,
                      SortManageAdapter.ItemOnClickListen listen, SortManageAdapter adapter) {
        super(itemView, context);
        mListen = listen;
        mAdapter = adapter;
    }


    @Override
    public void onBindView(final T bean, int position) {
        setView(bean, position);
    }


    private void setView(final T bean, int position){
        if (bean instanceof FirstSortBean) {
            mTvSort.setText(((FirstSortBean)bean).getFirst_sort_name());
        } else if (bean instanceof SecondSortBean) {
            mTvSort.setText(((SecondSortBean) bean).getSecond_sort_name());
        } else if (bean instanceof ThirdSortBean) {
            mTvSort.setText(((ThirdSortBean) bean).getThird_sort_name());
        }else {
            return;
        }

        setView2(bean, position);
    }


    private void setView2(T bean, final int position){
        setTvSortBackgroundColor(position);
        setItemClickListener(bean, position);
    }


    @SuppressLint("NewApi")
    private void setTvSortBackgroundColor(int position){
        if(mAdapter != null && mAdapter.getCurPosition() == position){
            mTvSort.setBackgroundColor(mContext.getColor(R.color.whitesmoke));
        }else {
            mTvSort.setBackgroundColor(mContext.getColor(R.color.white));
        }
    }

    private void setItemClickListener(final T bean, final int position){
        mTvSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListen != null){
                    mListen.itemOnClick(bean);
                    setCurPosition(position);
                }
            }
        });
    }

    private void setCurPosition(int position){
        if(mAdapter != null){
            mAdapter.setCurPosition(position);
            mAdapter.notifyDataSetChanged();
        }
    }
}
